package edu.bu.grocerylist.mock;

import java.util.ArrayList;
import java.util.List;

import edu.bu.grocerylist.dao.IGroceryUserDao;
import edu.bu.grocerylist.model.GroceryUser;

/**
 * MockDAO since we don't want our test code to call a live database
 * @author donlanp
 * 
 */
public class MockGroceryUserDao implements IGroceryUserDao {

	private List<GroceryUser> testData;

	/**
	 * Constructor that fills list with test data
	 */
	public MockGroceryUserDao() {
		testData = new ArrayList<GroceryUser>();
		
		// Insert new data

		for (int i = 0; i < 10; i++) {
			GroceryUser user = new GroceryUser(i + "@mail", "pass" + i,"question","answer");
			user.setUserId(i);
			testData.add(user);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getAllUsers()
	 */
	public List<GroceryUser> getAllUsers() {
		return testData;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.bu.grocerylist.dao.IGroceryUserDao#getUserByCredentials(java.lang
	 * .String, java.lang.String)
	 */
	public GroceryUser getUserByCredentials(String username, String password) {
		GroceryUser found = null;

		for (GroceryUser g : testData) {
			if (g.getEmail().equals(username)
					&& g.getPassword().equals(password)) {
				found = g;
			}
		}

		return found;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.bu.grocerylist.dao.IGroceryUserDao#getUsersByUsername(java.lang.String
	 * )
	 */
	public List<GroceryUser> getUsersByUsername(String username) {

		List<GroceryUser> found = new ArrayList<GroceryUser>();

		for (GroceryUser g : testData) {
			if (g.getEmail().equals(username)) {
				found.add(g);
			}
		}

		return found;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see edu.bu.grocerylist.dao.IGroceryUserDao#getUserByUserId(int)
	 */
	public GroceryUser getUserByUserId(int userId) {
		GroceryUser found = null;

		for (GroceryUser g : testData) {
			if (g.getUserId() == userId) {
				found = g;
			}
		}

		return found;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.bu.grocerylist.dao.IGroceryUserDao#saveUser(edu.bu.grocerylist.model
	 * .GroceryUser)
	 */
	public int saveUser(GroceryUser newUser) {
		for (GroceryUser g : testData) {
			if(newUser.getEmail().equals(g.getEmail())){
				throw new IllegalStateException("Cannot have duplicate Ids!");
			}
		}
		
		newUser.setUserId(testData.size() + 10);
		testData.add(newUser);
		
		return newUser.getUserId();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * edu.bu.grocerylist.dao.IGroceryUserDao#updateUser(edu.bu.grocerylist.
	 * model.GroceryUser)
	 */
	public int updateUser(GroceryUser userToUpdate) {
		GroceryUser found = null;

		for (GroceryUser g : testData) {
			if (g.getUserId() == userToUpdate.getUserId()) {
				found = g;
			}
		}
		
		if(found == null){
			throw new IllegalStateException("No user found!");
		} else {
			found.setEmail(userToUpdate.getEmail());
			found.setPassword(userToUpdate.getPassword());
			return 1;
		}

	}

	public GroceryUser getUserByUserId(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	public void deleteUser(GroceryUser user) {
		// TODO Auto-generated method stub
		
	}

}
